﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Data.SqlClient;


namespace FirmaTransportowa
{
    public partial class AdminAddNewUser : Form
    {

        SqlConnection dbConnection;

        private AdminPanelUserView m_parent;


        public AdminAddNewUser()
        {
            SqlConnector connection = new SqlConnector();
            dbConnection = connection.getInstance();
            InitializeComponent();
        }

        public AdminAddNewUser(AdminPanelUserView frm1)
        {
            InitializeComponent();
            m_parent = frm1;
        }

        private void label1_Click(object sender, EventArgs e)
        {

        }

        private void label3_Click(object sender, EventArgs e)
        {

        }

        private void addButton_Click(object sender, EventArgs e)
        {
            bool fieldsFilledOut = true;

            if (loginBox.Text.Trim() == String.Empty)
            {
                loginError.Text = "Podaj login";
                loginError.ForeColor = Color.DarkRed;
                fieldsFilledOut = false;
            }

            if (passwordBox.Text.Trim() == String.Empty)
            {
                passwordError.Text = "Podaj hasło";
                passwordError.ForeColor = Color.DarkRed;
                fieldsFilledOut = false;
            }

            if ((fieldsFilledOut) && (!checkIfRecordExists(loginBox.Text.Trim().ToString(), "Users")))
            {
                dbConnection.Open();
                SqlCommand cmd = new SqlCommand("INSERT INTO Users (login, password, worker_ID) VALUES (@login, @password, @worker_ID)");
                cmd.Connection = dbConnection;
                cmd.CommandType = CommandType.Text;
                cmd.Parameters.AddWithValue("@login", loginBox.Text.Trim().ToString());
                cmd.Parameters.AddWithValue("@password", passwordBox.Text.Trim().ToString());
                cmd.Parameters.AddWithValue("@worker_ID", dataGridView1.Rows[dataGridView1.CurrentRow.Index].Cells[2].Value.ToString());
                cmd.ExecuteNonQuery();
                dbConnection.Close();
               
            }

        }

        private void AdminAddNewUser_Shown(object sender, EventArgs e)
        {
            try
            {
                dbConnection.Open();
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.ToString());
            }
        }


        private void AdminAddNewUser_Load(object sender, EventArgs e)
        {
            // TODO: This line of code loads data into the 'transportDataSet1.Workers' table. You can move, or remove it, as needed.
            //this.workersTableAdapter1.Fill(this.transportDataSet1.Workers);
            // TODO: This line of code loads data into the 'transportDataSet.Workers' table. You can move, or remove it, as needed.
            //this.workersTableAdapter.Fill(this.transportDataSet.Workers);

        }

        private void listBox1_SelectedIndexChanged(object sender, EventArgs e)
        {

        }

        private void dataGridView1_CellContentClick(object sender, DataGridViewCellEventArgs e)
        {

        }

        public static bool checkIfRecordExists(string checkedText, string desiredTable)
        {
            bool result = false;

            SqlConnection dbConnection2;
            SqlConnector connection = new SqlConnector();
            dbConnection2 = connection.getInstance();
            dbConnection2.Open();

            try
            {
                string sql = "SELECT * FROM " + desiredTable + " WHERE login='" + checkedText + "'";

                SqlCommand cmd = new SqlCommand(sql, dbConnection2);
                SqlDataReader reader = cmd.ExecuteReader();

                if (reader.Read())
                {
                    result = true;
                    MessageBox.Show("Podany login juz istnieje w bazie!");
                }
                reader.Close();
            }
            finally
            {
                dbConnection2.Close();
            }

            return result;
        }


    }
}
